<?php
/**
 * 属性V1.0
 * 配方属性
 * @author qi
 * @date 2018/05/18
 *
 */
namespace app\admin\controller;

use controller\BasicAdmin;
use service\DataService;
use service\ToolsService;
use think\Db;

class Attribute extends BasicAdmin {

    public $table = 'SystemAttribute';

    /**
     *  分类列表
     */
    public function index() {
        $this->title = '配方属性管理';
//         $category = CategoryModel::getCategoryByAbbr("doc");
//         print_r($category);

//         $categoryList = CategoryModel::getChildren(0);
//         print_r($categoryList);

//         $categoryList = CategoryModel::getChildrenByAbbr("doc");
//         print_r($categoryList);
        list($get, $db) = [$this->request->get(), Db::name($this->table)];

        if (isset($get['title']) && $get['title'] !== '') {
            $db->whereLike('name', "%".$get['title']."%");
        }
        if (isset($get['desc']) && $get['desc'] !== '') {
            $db->whereLike('comment', "%".$get['desc']."%");
        }
        if (isset($get['abbr']) && $get['abbr'] !== '') {
            $db->whereLike('abbr', "%".$get['abbr']."%");
        }
//        if (isset($get['pid']) && $get['pid'] !== '') {
//            $db->whereOr('pid','in', $get['pid']);
//        }
        if (isset($get['pid']) && $get['pid'] !== '') {
            $db->whereOr('id','in', $get['pid']);
        }
        if (isset($get['id']) && $get['id'] !== '') {
            $db->whereOr('pid','in', $get['id']);
        }
        if (isset($get['id']) && $get['id'] !== '') {
            $db->whereOr('id', $get['id']);
        }
        return  parent::_list($db);
    }

    /**
     * 权限授权
     * @return string|array
     */
    public function apply() {
        $auth_id = $this->request->get('id', '0');
        switch (strtolower($this->request->get('action', '0'))) {
            case 'getnode':
                $nodes = NodeModel::get();
                $checked = Db::name('SystemAuthNode')->where('auth', $auth_id)->column('node');
                foreach ($nodes as $key => &$node) {
                    $node['checked'] = in_array($node['node'], $checked);
                    if (empty($node['is_auth']) && substr_count($node['node'], '/') > 1) {
                        unset($nodes[$key]);
                    }
                }
                $this->success('获取节点成功！', '', $this->_filterNodes($this->_filterNodes(ToolsService::arr2tree($nodes, 'node', 'pnode', '_sub_'))));
                break;
            case 'save':
                $data = [];
                $post = $this->request->post();
                foreach (isset($post['nodes']) ? $post['nodes'] : [] as $node) {
                    $data[] = ['auth' => $auth_id, 'node' => $node];
                }
                Db::name('SystemAuthNode')->where('auth', $auth_id)->delete();
                Db::name('SystemAuthNode')->insertAll($data);
                $this->success('节点授权更新成功！', '');
                break;
            default :
                $this->assign('title', '节点授权');
                return $this->_form($this->table, 'apply');
        }
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_data_filter(&$data) {
        $data = ToolsService::arr2table($data);
    }

    /**
     * 表单数据默认处理
     * @param array $data
     */
    public function _form_filter(&$data) {
        if ($this->request->isPost()) {
            $data['create_time'] = time();
        }else {
            $categorys = Db::name($this->table)->order('id desc')->select();
            $categorys = ToolsService::arr2table($categorys);
            if(isset($_GET['pid'])){
                $data['pid'] = $_GET['pid'];
            }
            $this->assign('category_root', $categorys);
        }
       
    }

    /**
     * 添加-属性
     */
    public function add() {
        return $this->_form($this->table, 'form');
    }

    /**
     * 编辑-属性
     */
    public function edit() {
        return $this->_form($this->table, 'form');
    }

    /**
     * 删除-属性
     */
    public function del() {
        if (DataService::update($this->table)) {
            $this->success("属性删除成功！", '');
        }
        $this->error("属性删除失败，请稍候再试！");
    }


}

